<template>
	<page-meta :page-style="show ? 'overflow: hidden;' : ''" />
	<view>
		<view class="add-roster-container">
			<scroll-view class="scroll-view" scroll-y>
				<view class="collapse-container" :class="!idInfoFlag ? 'close' : 'open'">
					<view class="collapse-item-title" @tap.stop="idInfoFlag = !idInfoFlag">
						<commonIcon class="title-icon" type="icon-zhengjianxinxi" :size="36" />
						<text class="title-content">证件信息</text>
						<image :src="collapseIcon" class="collapse-icon"></image>
					</view>
					<view class="collapse-item-content" v-show="idInfoFlag">
						<view class="info-list">
							<view class="info-item">
								<view class="info-label">姓名</view>
								<view class="info-content" v-if="!isScanWorkCode && !isRegister">
									{{ rosterInfo.name || rosterInfo.rosterName }}
								</view>
								<view class="info-content" v-else>
									<input type="text" v-model="rosterName" maxlength="20" placeholder="请输入" placeholder-class="placeholder-class" class="ipt" />
								</view>
							</view>
							<view class="info-item">
								<view class="info-label">性别</view>
								<view class="info-content">{{ rosterInfo.sexData }}</view>
							</view>
							<view class="info-item">
								<view class="info-label">民族</view>
								<view class="info-content">{{ rosterInfo.nation }}</view>
							</view>
							<view class="info-item">
								<view class="info-label">证件号</view>
								<view class="info-content">{{ rosterInfo.identityNumber }}</view>
							</view>
							<view class="info-item">
								<view class="info-label">发证机关</view>
								<view class="info-content">{{ rosterInfo.identityOrg }}</view>
							</view>
							<view class="info-item">
								<view class="info-label">出生日期</view>
								<view class="info-content">{{ rosterInfo.birthdayData }}</view>
							</view>
							<view class="info-item">
								<view class="info-label">详细地址</view>
								<view class="info-content">{{ rosterInfo.address }}</view>
							</view>
							<view class="info-item">
								<view class="info-label">有效期</view>
								<view class="info-content">{{ rosterInfo.issuedAtData }}-{{ rosterInfo.expiredAtData }}</view>
							</view>
							<view class="vertical-item">
								<view class="vertical-label">身份证正反面</view>
								<view class="vertical-content">
									<view class="pic-list">
										<preview-image
											class="pic-container"
											:avatar="rosterInfo.identityFrontPhotoData"
											:imageList="[rosterInfo.identityFrontPhotoData, rosterInfo.identityBackPhotoData]"
											preview-class="pic"
										></preview-image>
										<preview-image
											class="pic-container"
											:avatar="rosterInfo.identityBackPhotoData"
											:imageList="[rosterInfo.identityFrontPhotoData, rosterInfo.identityBackPhotoData]"
											preview-class="pic"
										></preview-image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="collapse-container" :class="!personInfoFlag ? 'close' : 'open'">
					<view class="collapse-item-title" @tap.stop="personInfoFlag = !personInfoFlag">
						<commonIcon class="title-icon" type="icon-gerenxinxi1" :size="36" />
						<text class="title-content">个人信息</text>
						<image :src="collapseIcon" class="collapse-icon"></image>
					</view>
					<view class="collapse-item-content" v-show="personInfoFlag">
						<view class="info-list">
							<view class="vertical-item" v-if="!isScanWorkCode && !isRegister">
								<view class="vertical-label">人脸近照</view>
								<view class="vertical-content">
									<view class="pic-list">
										<preview-image class="pic-container" :avatar="rosterInfo.avatarData" previewClass="pic"></preview-image>
									</view>
								</view>
							</view>
							<view class="vertical-item" v-else>
								<view class="vertical-label">人脸近照</view>
								<view class="vertical-content">
									<view class="pic-list">
										<multiple-upload-pic
											round
											:imgPath="[avatarShow]"
											:width="162"
											:height="162"
											data-type="avatar"
											@chooseimg="chooseImg($event, { type: 'avatar' })"
										></multiple-upload-pic>
									</view>
								</view>
							</view>
							<view class="info-item">
								<view class="info-label">
									<text class="require-icon">*</text>
									人员性质
								</view>
								<picker mode="selector" :value="selectPersonType" :range="personTypeList" range-key="value" class="picker-container" @change="changePersonType">
									<view class="choose-content-container">
										<view class="choose-content">{{ personTypeList[selectPersonType]?.value }}</view>
										<view class="right-arrow"></view>
									</view>
								</picker>
							</view>
							<view class="info-item">
								<view class="info-label">
									<text class="require-icon">*</text>
									组织机构
								</view>
								<view class="info-content" @tap="chooseOrg">{{ workTeamName }}</view>
								<view class="right-arrow"></view>
							</view>
							<view class="info-item" v-if="personTypeList[selectPersonType]?.code != '10'">
								<view class="info-label">
									<text class="require-icon">*</text>
									工种
								</view>
								<view class="info-content" @tap="toChooseWorkType">{{ worktypeName }}</view>
								<view class="right-arrow"></view>
							</view>
							<view class="info-item" v-else>
								<view class="info-label">
									<text class="require-icon">*</text>
									职位
								</view>
								<view class="info-content" @tap="toChooseStation">{{ stationName }}</view>
								<view class="right-arrow"></view>
							</view>

							<view class="info-item">
								<view class="info-label">
									<text class="require-icon">*</text>
									联系电话
								</view>
								<view class="info-content">
									<input type="number" placeholder="请输入" placeholder-class="placeholder-class" class="ipt" maxlength="11" v-model="phone" />
								</view>
							</view>
							<view class="info-item">
								<view class="info-label">
									<text class="require-icon">*</text>
									籍贯
								</view>
								<choose-area class="info-content" modelType="model2" @choosearea="chooseArea" :initArea="initArea" :initAreaName="showAreaName"></choose-area>
								<view class="right-arrow"></view>
							</view>
						</view>
					</view>
				</view>
				<view class="collapse-container" :class="!agreementFlag ? 'close' : 'open'">
					<view class="collapse-item-title" @tap.stop="agreementFlag = !agreementFlag">
						<commonIcon class="title-icon" type="icon-yonggongxieyi" :size="36" />
						<text class="title-content">用工协议</text>
						<image :src="collapseIcon" class="collapse-icon"></image>
					</view>
					<view class="collapse-item-content" v-show="agreementFlag">
						<view class="info-list">
							<view class="info-item" @tap="uploadFile" data-type="entryAgreement">
								<view class="info-label">进场承诺书(进场协议)</view>
								<view class="info-content">{{ rosterInfo.entryAgreementData }}</view>
								<view class="right-arrow"></view>
							</view>
							<view class="info-item" @tap="uploadFile" data-type="departureAgreement">
								<view class="info-label">出场承诺书(出场协议)</view>
								<view class="info-content">{{ rosterInfo.departureAgreementData }}</view>
								<view class="right-arrow"></view>
							</view>
							<view class="info-item" @tap="uploadFile" data-type="contractAttachment">
								<view class="info-label">合同附件</view>
								<view class="info-content">{{ rosterInfo.contractAttachmentData }}</view>
								<view class="right-arrow"></view>
							</view>
						</view>
					</view>
				</view>
				<view class="collapse-container" :class="!extraInfoFlag ? 'close' : 'open'">
					<view class="collapse-item-title" @tap.stop="extraInfoFlag = !extraInfoFlag">
						<commonIcon class="title-icon" type="icon-fujiaxinxi" :size="36" />
						<text class="title-content">附加信息</text>
						<image :src="collapseIcon" class="collapse-icon"></image>
					</view>
					<view class="collapse-item-content" v-show="extraInfoFlag">
						<view class="info-list">
							<view class="info-item">
								<view class="info-label">
									<text class="require-icon">*</text>
									政治面貌
								</view>
								<picker
									mode="selector"
									:value="politicalStatusSelect"
									:range="politicalStatusList"
									range-key="value"
									class="picker-container"
									@change="handleChangeSelect"
									data-type="politicalStatus"
								>
									<view class="choose-content-container">
										<view class="choose-content">{{ politicalStatusList[politicalStatusSelect]?.value }}</view>
										<view class="right-arrow"></view>
									</view>
								</picker>
							</view>
							<view class="info-item">
								<view class="info-label">
									<text class="require-icon">*</text>
									文化程度
								</view>
								<picker
									mode="selector"
									:value="educationSelect"
									:range="educationList"
									range-key="value"
									class="picker-container"
									@change="handleChangeSelect"
									data-type="education"
								>
									<view class="choose-content-container">
										<view class="choose-content">{{ educationList[educationSelect]?.value }}</view>
										<view class="right-arrow"></view>
									</view>
								</picker>
							</view>
							<view class="info-item">
								<view class="info-label">婚姻状况</view>
								<picker
									mode="selector"
									:value="marriageStatusSelect"
									:range="marriageStatusList"
									range-key="value"
									class="picker-container"
									@change="handleChangeSelect"
									data-type="marriageStatus"
								>
									<view class="choose-content-container">
										<view class="choose-content">{{ marriageStatusList[marriageStatusSelect]?.value }}</view>
										<view class="right-arrow"></view>
									</view>
								</picker>
							</view>
							<view class="info-item">
								<view class="info-label">是否有重大病史</view>
								<picker
									mode="selector"
									:value="hasMajorDiseaseSelect"
									:range="hasMajorDiseaseList"
									range-key="value"
									class="picker-container"
									@change="handleChangeSelect"
									data-type="hasMajorDisease"
								>
									<view class="choose-content-container">
										<view class="choose-content">{{ hasMajorDiseaseList[hasMajorDiseaseSelect]?.value }}</view>
										<view class="right-arrow"></view>
									</view>
								</picker>
							</view>
							<view class="info-item">
								<view class="info-label">保险状态</view>
								<picker
									mode="selector"
									:value="insureStatusSelect"
									:range="insureStatusList"
									range-key="value"
									class="picker-container"
									@change="handleChangeSelect"
									data-type="insureStatus"
								>
									<view class="choose-content-container">
										<view class="choose-content">{{ insureStatusList[insureStatusSelect]?.value }}</view>
										<view class="right-arrow"></view>
									</view>
								</picker>
							</view>
							<view class="info-item">
								<view class="info-label">紧急联系人</view>
								<view class="info-content">
									<input
										type="text"
										placeholder="请输入"
										placeholder-class="placeholder-class"
										class="ipt"
										v-model="linkMan"
										maxlength="20"
										@input="handleInput"
									/>
								</view>
							</view>
							<view class="info-item">
								<view class="info-label">紧急联系电话</view>
								<view class="info-content">
									<input v-model="linkManPhone" maxlength="11" type="number" placeholder="请输入" placeholder-class="placeholder-class" class="ipt" />
								</view>
							</view>
							<view class="info-item">
								<view class="info-label">开户行</view>
								<view class="info-content">
									<input type="text" v-model="accountBankName" maxlength="64" placeholder="请输入" placeholder-class="placeholder-class" class="ipt" />
								</view>
							</view>
							<view class="info-item">
								<view class="info-label">银行卡号</view>
								<view class="info-content">
									<input type="number" v-model="accountBankNumber" maxlength="32" placeholder="请输入" placeholder-class="placeholder-class" class="ipt" />
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="gray-block"></view>
			</scroll-view>
			<view class="common-btns-container" v-if="!isScanWorkCode && !isRegister">
				<view class="common-btn-container" hover-class="common-btn" @tap="submitRosterInfo">提交申请加入项目</view>
			</view>
			<view class="common-btns-container" v-if="isScanWorkCode && !isRegister">
				<view class="common-btn-container" hover-class="common-btn" @tap="inviteToProject">确认邀请加入项目</view>
			</view>
			<view class="common-btns-container" v-if="!isScanWorkCode && isRegister">
				<view class="common-btn-container" hover-class="common-btn" @tap="inviteToProject">提交</view>
			</view>
		</view>
		<choose-org
			:show="show"
			:personType="personTypeList[selectPersonType]?.code"
			@cancel="closeChooseOrg"
			@confirm="confirmChooseOrg"
			:initOrg="initOrg"
			:projectUuid="projectUuid"
		></choose-org>
	</view>
</template>

<script>
import commonIcon from '@/pagesA/components/common/icon/icon';
import previewImage from '@/pagesA/components/common/preview-image/preview-image';
import chooseOrg from '@/pagesA/components/roster/choose-org/choose-org';
import chooseArea from '@/pagesA/components/roster/choose-area/choose-area';
import noData from '@/pagesA/components/common/no-data/no-data';
import dialog from '@/pagesA/components/common/dialog/dialog';
import { common } from '@/pagesA/utils/common.js';
// import collapseIcon from '@/pagesA/assets/image/collapse-icon.png';
import multipleUploadPic from '@/pagesA/components/common/multiple-upload-pic/multiple-upload-pic';
import moment from 'moment';
const app = getApp();
export default {
	components: {
		commonIcon,
		previewImage,
		chooseOrg,
		chooseArea,
		noData,
		dialog,
		multipleUploadPic
	},
	data() {
		return {
			collapseIcon: common.imgs.collapseIcon,
			idInfoFlag: false,
			personInfoFlag: true,
			agreementFlag: true,
			extraInfoFlag: true,
			activeNames: ['2', '3', '4', '5'],
			projectUuid: '',
			rosterInfo: {
				certificateList: '',
				province: '',
				city: '',
				town: '',
				name: '',
				rosterName: '',
				sexData: '',
				nation: '',
				identityNumber: '',
				identityOrg: '',
				birthdayData: '',
				address: '',
				issuedAtData: '',
				expiredAtData: '',
				identityFrontPhoto: '',
				identityBackPhoto: '',
				avatar: '',
				entryAgreementData: '',
				departureAgreementData: '',
				contractAttachmentData: ''
			},

			educationList: [],
			politicalStatusList: [],
			insureStatusList: [],
			marriageStatusList: [],
			hasMajorDiseaseList: [
				{
					code: 0,
					value: '否'
				},
				{
					code: 1,
					value: '是'
				}
			],
			personTypeList: [],
			selectPersonType: 0,
			worktypeCode: '',
			worktypeName: '',
			stationCode: '',
			stationName: '',
			workTeamUuid: '',
			workTeamName: '',
			initOrg: {},
			show: false,
			initArea: [],
			showAreaName: '',
			uploadFileLoading: false,
			politicalStatusSelect: 0,
			educationSelect: 0,
			marriageStatusSelect: 0,
			insureStatusSelect: 0,
			hasMajorDiseaseSelect: 0,
			linkMan: '',
			linkManPhone: '',
			accountBankName: '',
			accountBankNumber: '',
			phone: '',
			isScanWorkCode: false,
			inviteRosterInfo: {},
			showDialog: false,
			errTips: '',
			identityFrontPhotoShow: '',
			identityBackPhotoShow: '',
			avatarShow: '',
			rosterName: '',
			isRegister: false,
			value: '',
			code: '',
			unitType: '' //选择组织机构返回，仅针对市建项目，用于筛选职位列表
		};
	},
	onLoad(options) {
		this.projectUuid = options.projectUuid;
		this.isScanWorkCode = !!options.isScanWorkCode; //工友码扫码加入
		this.isRegister = !!options.isRegister; //是否通过人员登记进入
		console.log(app.globalData);
		delete app.globalData.chooseStation;
		delete app.globalData.chooseWorkType;
		this.initData();
	},
	onShow() {
		if (app.globalData.hasChooseStation) {
			//选择了职位
			// this.setData({
			// 	stationCode: app.globalData.chooseStation.code,
			// 	stationName: app.globalData.chooseStation.name
			// });
			this.stationCode = app.globalData.chooseStation.code;
			this.stationName = app.globalData.chooseStation.name;
			delete app.globalData.hasChooseStation;
		}
		if (app.globalData.hasChooseWorkType) {
			//选择了工种
			// this.setData({
			// 	worktypeCode: app.globalData.chooseWorkType.code,
			// 	worktypeName: app.globalData.chooseWorkType.name
			// });
			this.worktypeCode = app.globalData.chooseWorkType.code;
			this.worktypeName = app.globalData.chooseWorkType.name;
			delete app.globalData.hasChooseWorkType;
		}
		if (app.globalData.isAddCertificateInfo) {
			//新增证书
			this.rosterInfo.certificateList.push(app.globalData.certificateInfo);
			this.rosterInfo.certificateList = this.handleCertificateList(this.rosterInfo.certificateList);
			// this.setData({
			// 	['rosterInfo.certificateList']: this.rosterInfo.certificateList
			// });
			this.rosterInfo.certificateList = this.rosterInfo.certificateList;
			delete app.globalData.isAddCertificateInfo;
		}
		if (app.globalData.isEditCertificateInfo) {
			//编辑证书
			this.rosterInfo.certificateList[app.globalData.certificateIndex] = app.globalData.certificateInfo;
			this.rosterInfo.certificateList = this.handleCertificateList(this.rosterInfo.certificateList);
			// this.setData({
			// 	['rosterInfo.certificateList']: this.rosterInfo.certificateList
			// });
			this.rosterInfo.certificateList = this.rosterInfo.certificateList;
			delete app.globalData.isEditCertificateInfo;
			delete app.globalData.certificateIndex;
		}
	},
	methods: {
		open(e) {
			// console.log('open', e)
		},
		close(e) {
			// console.log('close', e)
		},
		change(e) {
			// console.log('change', e)
		},
		initData() {
			let rosterInfo = {};
			if (app.globalData.hasDetailInfo) {
				rosterInfo = app.globalData.detailInfo;
				rosterInfo.sexData = this.handlSex(rosterInfo.sex);
				rosterInfo.birthdayData = this.handlTime(rosterInfo.birthday, 'YYYY/MM/DD');
				rosterInfo.issuedAtData = this.handlTime(rosterInfo.issuedAt, 'YYYY.MM.DD');
				rosterInfo.expiredAtData = this.handleExpiredAt(rosterInfo.expiredAt);
				rosterInfo.entryAgreement = '';
				rosterInfo.departureAgreement = '';
				rosterInfo.contractAttachment = '';
				rosterInfo.entryAgreementData = '';
				rosterInfo.departureAgreementData = '';
				rosterInfo.contractAttachmentData = '';
				rosterInfo.certificateList = this.handleCertificateList(rosterInfo.certificateList);
				let worktypeName = rosterInfo.worktype ? rosterInfo.worktype.name : '';
				let worktypeCode = rosterInfo.worktype ? rosterInfo.worktype.code : '';
				let stationCode = rosterInfo.station ? rosterInfo.station.code : '';
				let stationName = rosterInfo.station ? rosterInfo.station.name : '';
				let phone = rosterInfo.phone;
				let initArea = [rosterInfo.province, rosterInfo.city, rosterInfo.town];
				let showAreaName = rosterInfo.nativePlace;
				let linkMan = rosterInfo.linkMan;
				let linkManPhone = rosterInfo.linkManPhone;
				let accountBankName = rosterInfo.accountBankName;
				let accountBankNumber = rosterInfo.accountBankNumber;
				let identityFrontPhotoShow = rosterInfo.identityFrontPhoto;
				let identityBackPhotoShow = rosterInfo.identityBackPhoto;
				let avatarShow = rosterInfo.avatarData;
				let rosterName = rosterInfo.name || rosterInfo.rosterName;
				if (rosterInfo.worktype) {
					app.globalData.chooseWorkType = rosterInfo.worktype;
				}
				if (rosterInfo.station) {
					app.globalData.chooseStation = rosterInfo.station;
				}
				// this.setData({
				// 	rosterInfo,
				// 	rosterName,
				// 	worktypeName,
				// 	worktypeCode,
				// 	stationCode,
				// 	stationName,
				// 	phone,
				// 	initArea,
				// 	showAreaName,
				// 	linkMan,
				// 	linkManPhone,
				// 	accountBankName,
				// 	accountBankNumber,
				// 	isScanWorkCode: this.isScanWorkCode,
				// 	projectUuid: this.projectUuid,
				// 	isRegister: this.isRegister,
				// 	identityFrontPhotoShow,
				// 	identityBackPhotoShow,
				// 	avatarShow
				// });
				this.rosterInfo = rosterInfo;
				this.rosterName = rosterName;
				this.worktypeName = worktypeName;
				this.worktypeCode = worktypeCode;
				this.stationCode = stationCode;
				this.stationName = stationName;
				this.phone = phone;
				this.initArea = initArea;
				this.showAreaName = showAreaName;
				this.linkMan = linkMan;
				this.linkManPhone = linkManPhone;
				this.accountBankName = accountBankName;
				this.accountBankNumber = accountBankNumber;
				this.identityFrontPhotoShow = identityFrontPhotoShow;
				this.identityBackPhotoShow = identityBackPhotoShow;
				this.avatarShow = avatarShow;
				// this.selectComponent("#chooseArea").setData({
				//   showAreaName: rosterInfo.nativePlace
				// })
				this.getPropertiesForChoose();
				if (!uni.getStorageSync('cisdiHwConfig')) {
					common.commonMethods.getOBSConfig();
				}
				delete app.globalData.hasDetailInfo;
				delete app.globalData.detailInfo;
			}
		},

		handleInput() {},

		handlSex(value) {
			if (value == 0) {
				return '男';
			} else if (value == 1) {
				return '女';
			}
			return;
		},

		handlTime(value, format) {
			return value ? moment(value).format(format) : '';
		},

		handleExpiredAt(expiredAt) {
			//4100688000000为后端设定的一个时间戳，等于就意味着身份证长期有效
			if (expiredAt == 4100688000000) {
				return '长期';
			}
			return this.handlTime(expiredAt, 'YYYY.MM.DD');
		},

		handleCertificateList(certificateList) {
			for (let i = 0; i < certificateList.length; i++) {
				certificateList[i].indexData = '证书' + common.commonMethods.toChinesNum(i + 1);
				certificateList[i].issuedAtData = this.handlTime(certificateList[i].issuedAt, 'YYYY/MM/DD');
				certificateList[i].expiredAtData = this.handlTime(certificateList[i].expiredAt, 'YYYY/MM/DD');
			}
			return certificateList;
		},
		handlePersonTypeIndex(personTypeList, personType) {
			if (!personType) {
				return;
			}
			for (let i = 0; i < personTypeList.length; i++) {
				if (personTypeList[i].code == personType) {
					return i;
				}
			}
			return 0;
		},
		getPropertiesForChoose() {
			common.apis.project.getPropertiesForChoose().then((res) => {
				if (!res.data) {
					return;
				}
				let selectPersonType = this.handlePersonTypeIndex(res.data.personType, this.rosterInfo?.personType?.code);
				let politicalStatusSelect = this.handleInitSelect('politicalStatus', res.data.politicalStatus);
				let educationSelect = this.handleInitSelect('education', res.data.education);
				let marriageStatusSelect = this.handleInitSelect('marriageStatus', res.data.insureStatus);
				let insureStatusSelect = this.handleInitSelect('insureStatus', res.data.insureStatus);
				let hasMajorDiseaseSelect = this.rosterInfo.hasMajorDisease ? 1 : 0;
				// this.setData({
				// 	selectPersonType,
				// 	politicalStatusSelect,
				// 	educationSelect,
				// 	marriageStatusSelect,
				// 	insureStatusSelect,
				// 	hasMajorDiseaseSelect,
				// 	educationList: res.data.education,
				// 	politicalStatusList: res.data.politicalStatus,
				// 	insureStatusList: res.data.insureStatus,
				// 	marriageStatusList: res.data.marriageStatus,
				// 	personTypeList: res.data.personType
				// });
				this.selectPersonType = selectPersonType;
				this.politicalStatusSelect = politicalStatusSelect;
				this.educationSelect = educationSelect;
				this.marriageStatusSelect = marriageStatusSelect;
				this.insureStatusSelect = insureStatusSelect;
				this.hasMajorDiseaseSelect = hasMajorDiseaseSelect;
				this.educationList = res.data.education;
				this.politicalStatusList = res.data.politicalStatus;
				this.insureStatusList = res.data.insureStatus;
				this.marriageStatusList = res.data.marriageStatus;
				this.personTypeList = res.data.personType;
			});
		},
		changePersonType(e) {
			const index = e.detail.value;
			if (index != this.selectPersonType && (this.personTypeList[index].code == '10' || this.personTypeList[this.selectPersonType].code == '10')) {
				//管理人员和非管理人员转换
				// this.setData({
				// 	worktypeCode: '',
				// 	worktypeName: '',
				// 	stationCode: '',
				// 	stationName: '',
				// 	workTeamUuid: '',
				// 	workTeamName: '',
				// 	initOrg: {},
				// 	selectPersonType: index
				// });
				this.worktypeCode = '';
				this.worktypeName = '';
				this.stationCode = '';
				this.stationName = '';
				this.workTeamUuid = '';
				this.workTeamName = '';
				this.initOrg = {};
				this.selectPersonType = index;
				delete app.globalData.chooseStation;
				delete app.globalData.chooseWorkType;
			} else {
				// this.setData({
				// 	selectPersonType: index
				// });
				this.selectPersonType = index;
			}
		},
		toChooseWorkType() {
			uni.navigateTo({
				url: '/pagesA/pages/roster/chooseWorkType/chooseWorkType?projectUuid=' + this.projectUuid
			});
		},
		toChooseStation() {
			if (!this.workTeamUuid) {
				common.message.toast('请先选择组织机构');
				return;
			}
			uni.navigateTo({
				url: '/pagesA/pages/roster/chooseStation/chooseStation?projectUuid=' + this.projectUuid + '&unitType=' + this.unitType
			});
		},
		chooseImg(e, options) {
			this.rosterInfo[`${options.type}`] = e.detail.filePath[0];
			this[`${options.type}Show`] = e.detail.showImgPath[0];
		},
		chooseOrg() {
			// this.setData({
			// 	show: true
			// });
			this.show = true;
		},
		confirmChooseOrg(res) {
			// this.setData({
			// 	initOrg: {
			// 		orgId: res.detail.orgId
			// 	},
			// 	workTeamUuid: res.detail.orgId,
			// 	workTeamName: res.detail.orgNameData
			// });
			this.initOrg.orgId = res.detail.orgId;
			this.initOrg.unitType = res.detail.unitType;
			this.workTeamUuid = res.detail.orgId;
			this.workTeamName = res.detail.orgNameData;
			if (res.detail.unitType != this.unitType) {
				this.stationCode = '';
				this.stationName = '';
				delete app.globalData.chooseStation;
			}
			this.unitType = res.detail.unitType;
		},
		closeChooseOrg() {
			// this.setData({
			// 	show: false
			// });

			this.show = false;
		},
		chooseArea(res) {
			// this.setData({
			// 	initArea: res.detail.code,
			// 	['rosterInfo.province']: res.detail.code[0],
			// 	['rosterInfo.city']: res.detail.code[1],
			// 	['rosterInfo.town']: res.detail.code[2]
			// });
			this.initArea = res.detail.code;
			this.rosterInfo.province = res.detail.code[0];
			this.rosterInfo.city = res.detail.code[1];
			this.rosterInfo.town = res.detail.code[2];
		},

		handleUrl(key) {
			let word = '';
			if (!key) {
				return word;
			}
			let keyLen = key.length;
			let leftNum = 20;
			let rightNum = 8;
			if (keyLen > leftNum + rightNum) {
				word += key.substring(0, leftNum);
				word += '...';
				word += key.substring(keyLen - rightNum, keyLen + 1);
			}
			return word;
		},
		uploadFile(e) {
			if (this.uploadFileLoading) {
				return;
			}
			this.uploadFileLoading = true;
			const type = e.currentTarget.dataset.type;
			common.commonMethods.uploadFile(['pdf'], (res) => {
				this.uploadFileLoading = false;
				if (res.code == 0) {
					let url = this.handleUrl(res.url);
					// this.setData({
					// 	[`rosterInfo.${type}`]: res.url,
					// 	[`rosterInfo.${type}Data`]: url
					// });
					this.rosterInfo[`${type}`] = res.url;
					this.rosterInfo[`${type}Data`] = url;
				}
			});
		},

		handleInitSelect(type, list) {
			if (this.rosterInfo[type]) {
				for (let i = 0; i < list.length; i++) {
					if (list[i].code == this.rosterInfo[type].code) {
						return i;
					}
				}
			}
			return -1;
		},

		handleChangeSelect(e) {
			const type = e.currentTarget.dataset.type;
			const index = e.detail.value;
			// this.setData({
			// 	[`${type}Select`]: index,
			// 	[`rosterInfo.${type}`]: this[type + 'List'][index].code
			// });
			this[`${type}Select`] = index;
			this.rosterInfo[`${type}`] = this[type + 'List'][index].code;
		},

		delCertificate(e) {
			const index = e.currentTarget.dataset.index;
			let item = this.rosterInfo.certificateList[index];
			uni.showModal({
				title: '温馨提示',
				content: `是否删除${item.name}`,
				complete: (res) => {
					if (res.confirm) {
						this.rosterInfo.certificateList.splice(index, 1);
						this.rosterInfo.certificateList = this.handleCertificateList(this.rosterInfo.certificateList);
						// this.setData({
						// 	['rosterInfo.certificateList']: this.rosterInfo.certificateList
						// });
						this.rosterInfo.certificateList = this.rosterInfo.certificateList;
					}
				}
			});
		},

		toAddCertificate() {
			uni.navigateTo({
				url: '/pages/roster/certificate/editCertificate/editCertificate'
			});
		},

		toEditCertificate(e) {
			const item = e.currentTarget.dataset.item;
			const index = e.currentTarget.dataset.index;
			app.globalData.hasCertificateInfo = true;
			app.globalData.certificateInfo = item;
			app.globalData.certificateIndex = index;
			uni.navigateTo({
				url: '/pages/roster/certificate/editCertificate/editCertificate?type=edit'
			});
		},

		handleSubmitSelect(index, type) {
			if (index != -1) {
				this.rosterInfo[type] = this[type + 'List'][index]?.code;
			} else {
				this.rosterInfo[type] = '';
			}
		},

		handlePostData(info) {
			for (let key in info) {
				if (info[key] !== 0 && !info[key]) {
					delete info[key];
				}
			}
			return info;
		},

		checkPostData() {
			let rosterInfo = this.rosterInfo;
			rosterInfo.name = this.rosterName;
			rosterInfo.personType = this.personTypeList[this.selectPersonType]?.code;
			rosterInfo.worktypeCode = this.worktypeCode;
			rosterInfo.stationCode = this.stationCode;
			rosterInfo.workTeamUuid = this.workTeamUuid;
			rosterInfo.phone = this.phone;
			this.handleSubmitSelect(this.politicalStatusSelect, 'politicalStatus');
			this.handleSubmitSelect(this.educationSelect, 'education');
			this.handleSubmitSelect(this.marriageStatusSelect, 'marriageStatus');
			this.handleSubmitSelect(this.hasMajorDiseaseSelect, 'hasMajorDisease');
			this.handleSubmitSelect(this.insureStatusSelect, 'insureStatus');
			rosterInfo.linkMan = this.linkMan;
			rosterInfo.linkManPhone = this.linkManPhone;
			rosterInfo.accountBankName = this.accountBankName;
			rosterInfo.accountBankNumber = this.accountBankNumber;
			rosterInfo.credentialType = rosterInfo.credentialType?.code || '';
			rosterInfo.religion = rosterInfo.religion?.code || '';
			rosterInfo.nationality = rosterInfo.nationality?.code || '';
			if (!rosterInfo.name) {
				common.message.toast('请输入姓名');
				return;
			}
			if (!rosterInfo.personType) {
				common.message.toast('请选择人员性质');
				return;
			}
			if (rosterInfo.personType == '10' && !rosterInfo.stationCode) {
				common.message.toast('请选择职位');
				return;
			}
			if (rosterInfo.personType != '10' && !rosterInfo.worktypeCode) {
				common.message.toast('请选择工种');
				return;
			}
			if (!rosterInfo.workTeamUuid) {
				common.message.toast('请选择组织机构');
				return;
			}
			if (!rosterInfo.phone) {
				common.message.toast('请输入联系电话');
				return;
			}
			if (!rosterInfo.province) {
				common.message.toast('请选择籍贯');
				return;
			}
			if (!rosterInfo.politicalStatus) {
				common.message.toast('请选择政治面貌');
				return;
			}
			if (!rosterInfo.education) {
				common.message.toast('请选择文化程度');
				return;
			}
			delete rosterInfo.station;
			delete rosterInfo.worktype;
			delete rosterInfo.projectUuid;
			rosterInfo = this.handlePostData(rosterInfo);
			return rosterInfo;
		},

		submitRosterInfo(e) {
			//调用的是加入项目接口，且没有大龄和超龄校验（前置页面已进行大龄超龄校验）
			let rosterInfo = {};
			rosterInfo = this.checkPostData();
			if (!rosterInfo) {
				return;
			}
			//e.type的意思是如果是按钮直接点击，则会有type值，也就是isForceEnter：0，如果是直接调用方法，就不会存在type值，也就是isForceEnter：1
			rosterInfo.isForceEnter = e.type ? 0 : 1;
			uni.showLoading({
				title: '提交中',
				mask: true
			});
			common.apis.project
				.submitRosterInfo({
					...rosterInfo,
					projectUuid: this.projectUuid
				})
				.then(
					() => {
						common.message.toast('申请成功');
						setTimeout(() => {
							if (this.isRegister) {
								uni.navigateBack({
									delta: 3
								});
							} else {
								uni.navigateBack();
							}
						}, 1500);
					},
					(err) => {
						uni.hideLoading();
						common.commonMethods.addRosterCheck(
							err,
							() => {
								this.submitRosterInfo(1);
							},
							() => {},
							() => {
								common.message.toast(err.msg);
							}
						);
					}
				);
		},

		// checkAge(rosterInfo) {
		//   app.apis.roster.checkAge({
		//     identityNumber: rosterInfo.identityNumber,
		//     personType: rosterInfo.personType,
		//   }).then(res => {
		//     if (!res.data) return;
		//     if (res.data.state) {
		//       this.setData({
		//         showDialog: true,
		//         errTips: res.data.message,
		//         inviteRosterInfo: rosterInfo
		//       });
		//       return;
		//     }
		//     this.submitInviteRosterInfo(rosterInfo);
		//   })
		// },
		submitInviteRosterInfo(inviteRosterInfo) {
			uni.showLoading({
				title: '提交中',
				mask: true
			});
			common.apis.roster.improve(inviteRosterInfo).then(
				() => {
					//返回首页
					common.message.toast('提交成功');
					setTimeout(() => {
						if (this.isRegister) {
							uni.navigateBack({
								delta: 3
							});
						} else {
							uni.switchTab({
								url: '/pages/workSite/index'
							});
						}
					}, 1500);
				},
				(err) => {
					uni.hideLoading();
					common.commonMethods.addRosterCheck(
						err,
						() => {
							this.inviteToProject(1);
						},
						() => {},
						() => {
							common.message.toast(err.msg);
						}
					);
				}
			);
		},

		inviteToProject(e) {
			//调用的是新增人员接口，且多了一个大龄和超龄校验
			let rosterInfo = {};
			rosterInfo = this.checkPostData();
			if (!rosterInfo) {
				return;
			}
			//e.type的意思是如果是按钮直接点击，则会有type值，也就是isForceEnter：0，如果是直接调用方法，就不会存在type值，也就是isForceEnter：1
			rosterInfo.isForceEnter = e.type ? 0 : 1;
			// this.checkAge(rosterInfo);
			//后端修改逻辑，把大龄、超龄和黑名单的校验都放到了提交请求里面
			this.submitInviteRosterInfo(rosterInfo);
		},

		confirm() {
			this.submitInviteRosterInfo(this.inviteRosterInfo);
		},

		closeDialog() {
			// this.setData({
			// 	showDialog: false
			// });
			this.showDialog = false;
		}
	}
};
</script>
<style>
@import url('@/pagesA/assets/style/common.css');
.add-roster-container {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: #f6f7fa;
}

.scroll-view {
	flex: 1;
	height: 0;
}

.collapse-item-title {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #4b5680;
	padding: 16rpx 32rpx;
}

.collapse-item-title .title-icon {
	margin-right: 12rpx;
	width: 36rpx;
	height: 36rpx;
}
.collapse-item-content {
	background: #fff;
}
.open .collapse-icon {
	transform: rotate(180deg);
	transition: all 0.3s;
}
.close .collapse-icon {
	transition: all 0.3s;
}
.collapse-icon {
	width: 40rpx;
	height: 40rpx;
	margin-left: auto;
}

.info-list {
	padding: 0 32rpx;
}

.info-item,
.vertical-item {
	border-bottom: 1rpx solid #e1e2e7;
	font-size: 32rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
}

.info-item {
	display: flex;
	align-items: center;
	min-height: 116rpx;
}

.info-list .info-item:last-child,
.info-list .vertical-item:last-child {
	border-bottom: 0;
}

.info-item .info-label,
.vertical-item .vertical-label {
	color: #1e253f;
	margin-right: 32rpx;
	flex-shrink: 0;
	display: flex;
	align-items: center;
}

.info-label .require-icon {
	color: #ff3838;
	margin-right: 8rpx;
}

.info-item .info-content {
	margin-left: auto;
	min-height: 116rpx;
	flex: 1;
	color: #4b5680;
	text-align: right;
	display: flex;
	align-items: center;
	justify-content: flex-end;
}

.info-content .ipt {
	width: 100%;
	height: 100%;
}

.picker-container {
	width: 100%;
	height: 100%;
}

.choose-content-container {
	height: 116rpx;
	display: flex;
	align-items: center;
}

.choose-content-container .choose-content {
	flex: 1;
	height: 100%;
	color: #4b5680;
	display: flex;
	align-items: center;
	justify-content: flex-end;
}

.placeholder-class {
	font-size: 32rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
}

.info-item .right-arrow {
	flex-shrink: 0;
	width: 15rpx;
	height: 15rpx;
	border-top: 4rpx solid #949dbf;
	border-right: 4rpx solid #949dbf;
	transform: rotate(45deg);
	margin-left: 6rpx;
}

.vertical-item .vertical-label {
	padding-top: 28rpx;
}

.vertical-item .vertical-content {
	padding: 28rpx 0;
}

.vertical-content .pic-list {
	display: flex;
	align-items: center;
}

.pic-list .pic-container {
	width: 162rpx;
	height: 162rpx;
	margin-right: 24rpx;
	border-radius: 6rpx;
}

.pic-list .pic {
	width: 158rpx;
	height: 158rpx;
	border-radius: 6rpx;
	border: 1rpx solid #edeff7;
}

.certificate-list .certificate-item {
	padding: 24rpx 32rpx;
	border-bottom: 1px solid #e1e2e7;
}

.certificate-list .certificate-item:last-child {
	border-bottom: 0;
}

.certificate-item .certificate-item-top {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.certificate-item-top .certificate-title {
	font-size: 32rpx;
	font-family: PingFangSC-Medium, PingFang SC;
	font-weight: 550;
	color: #1e253f;
	display: flex;
	align-items: center;
}

.certificate-item .certificate-title::before {
	content: '';
	width: 6rpx;
	height: 28rpx;
	background: #3f64fa;
	border-radius: 3rpx;
	margin-right: 12rpx;
	position: relative;
	bottom: 2rpx;
}

.certificate-item .del-icon {
	width: 40rpx;
	height: 40rpx;
}

.certificate-item .certificate-content {
	display: flex;
	align-items: center;
	padding-top: 32rpx;
}

.certificate-content .certificate-name {
	font-size: 32rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #4b5680;
}

.certificate-content .right-arrow {
	margin-left: auto;
	flex-shrink: 0;
	width: 15rpx;
	height: 15rpx;
	border-top: 4rpx solid #949dbf;
	border-right: 4rpx solid #949dbf;
	transform: rotate(45deg);
}

.no-data {
	padding: 30rpx 0 0;
}

.add-certificate-btn {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 16rpx 0;
	font-size: 32rpx;
	font-family: PingFangSC-Medium, PingFang SC;
	font-weight: 550;
	color: #3f64fa;
}

.no-data .add-certificate-btn {
	margin-top: 30rpx;
	border-top: 1rpx solid #e1e2e7;
}

.gray-block {
	height: 16rpx;
	background-color: transparent;
}

.tip-content {
	padding: 40rpx 48rpx;
	font-size: 34rpx;
	font-family: PingFangSC-Medium, PingFang SC;
	font-weight: 550;
	color: #1e253f;
	text-align: center;
}
</style>
